package com.base.commonlib.base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.presenter.BasePresenter;
import com.base.commonlib.sp.SpUtil;

/**
 * Date：2019/4/11 上午10:22
 * Creator：arvin
 * Des：
 */

public abstract class CBaseMvpFragment<H extends BasePresenter> extends BaseViewFragment<H> {

    //是否可见
    protected Boolean isFragmentVisible = false;
    //activity是否初始化完成
    protected boolean isActivityPrepared = false;
    //是否是第一次加载
    protected boolean isDataFirstInit = true;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
        start();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isActivityPrepared = true;
        loadData();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser == true) {//当可见的时候执行操作
            isFragmentVisible = true;
            loadData();
        } else {//不可见时执行相应的操作
            isFragmentVisible = false;
            onInvisible();
        }
    }

    public abstract void init();

    public abstract void start();

    protected void loadData() {
        //当fragment可见、activity初始化完成、第一次加载时执行
        if (isActivityPrepared && isFragmentVisible && isDataFirstInit) {
            isDataFirstInit = false;
            lazyLoad();
        }
    }
    protected void onInvisible() {
    }

    protected void Login(){
        ARouter.getInstance().build("/mine/pwdlogin").navigation();
    }
    protected void lazyLoad(){};//子类实现

}
